using System;
using System.Collections.Generic;
using UnityEngine;

public class DartModelConfig : ScriptableObject
{
	[Serializable]
	public class DartPart
	{
		public DartPropertyType type;

		public List<GameObject> model = new List<GameObject>();

		public List<ModelLook> look = new List<ModelLook>();

		public FloatRange difficultyMultRange { get; protected set; }

		public void Init()
		{
			FloatRange floatRange = default(FloatRange);
			floatRange.max = (floatRange.min = look[0].difficultyMult);
			int num = 0;
			foreach (ModelLook item in look)
			{
				item.Init(this, num);
				floatRange.max = Mathf.Max(item.difficultyMult, floatRange.max);
				floatRange.min = Mathf.Min(item.difficultyMult, floatRange.min);
				num++;
			}
			difficultyMultRange = floatRange;
		}

		public void ApplyLook(DartAssembler dartAssembler, int lookIndex)
		{
			ModelLook modelLook = look[Mathf.Clamp(lookIndex, 0, look.Count - 1)];
			Debug.Log(modelLook.modelID);
			GameObject gameObject = null;
			if (dartAssembler.HasPart(type, modelLook.modelID))
			{
				Debug.Log("Has part");
				gameObject = dartAssembler.GetPart(type);
			}
			else
			{
				Debug.Log("Doesn't have part");
				gameObject = CreateDartPart(modelLook);
				dartAssembler.RemovePart(type);
				dartAssembler.AddPart(gameObject, type, modelLook.modelID);
			}
			ChangeTexture(gameObject, modelLook);
		}

		private GameObject CreateDartPart(ModelLook newLook)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(model[newLook.modelID]);
			ChangeTexture(gameObject, newLook);
			return gameObject;
		}

		private void ChangeTexture(GameObject dartPart, ModelLook newLook)
		{
			Texture texture = newLook.FetchTexture(type);
			for (int i = 0; i < dartPart.transform.childCount; i++)
			{
				Renderer component = dartPart.transform.GetChild(i).GetComponent<Renderer>();
				if (component != null && texture != null)
				{
					component.material.mainTexture = texture;
				}
			}
		}

		public ModelLook GetLook(int index)
		{
			return look[Mathf.Clamp(index, 0, look.Count - 1)];
		}
	}

	[Serializable]
	public class ModelLook
	{
		public int modelID;

		public string textureName;

		public int dollarPrice;

		public int coinPrice;

		public float difficultyMult = 1f;

		public int index;

		public string type;

		public void Init(DartPart part, int index)
		{
			this.index = index;
			type = part.type.ToString();
		}

		public Texture FetchTexture(DartPropertyType type)
		{
			if (!string.IsNullOrEmpty(textureName))
			{
				string text = "DartTextures/";
				switch (type)
				{
				case DartPropertyType.BarrelModel:
					text += "BARRELS 1/";
					break;
				case DartPropertyType.FlightModel:
					text += "FLIGHTS 1/";
					break;
				case DartPropertyType.ShaftModel:
					text += "SHAFTS 1/";
					break;
				}
				return Resources.Load<Texture>(text + textureName);
			}
			return null;
		}

		public bool isOwned()
		{
			return PlayerInventory.instance.isOwned(type + ":" + index);
		}
	}

	protected static DartModelConfig instance_;

	public List<DartPart> dartModels = new List<DartPart>();

	public static DartModelConfig instance
	{
		get
		{
			if (instance_ == null)
			{
				instance_ = Resources.Load("DartModelConfig", typeof(DartModelConfig)) as DartModelConfig;
				instance_.Init();
			}
			return instance_;
		}
	}

	public FloatRange difficultyMultRange { get; protected set; }

	public DartPart GetDartPart(int index)
	{
		if (index < dartModels.Count)
		{
			return dartModels[index];
		}
		return null;
	}

	protected void Init()
	{
		FloatRange floatRange = default(FloatRange);
		floatRange.min = 1f;
		floatRange.max = 1f;
		FloatRange floatRange2 = floatRange;
		foreach (DartPart dartModel in dartModels)
		{
			dartModel.Init();
			floatRange2.min *= dartModel.difficultyMultRange.min;
			floatRange2.max *= dartModel.difficultyMultRange.max;
		}
		difficultyMultRange = floatRange2;
	}

	public void CreateDart(DartAssembler dartAssembler)
	{
	}

	public void DeleteDart(DartAssembler dartAssembler)
	{
		foreach (DartPart dartModel in dartModels)
		{
			dartAssembler.RemovePart(dartModel.type);
		}
	}
}
